package com.hospital.warehouse.drugpurchase.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.util.DTO;
import com.hospital.util.JMyUtil;
import com.hospital.warehouse.drugpurchase.dao.DrugpurchaseDao;
import com.hospital.warehouse.drugpurchase.entity.Drugpurchase;
import com.hospital.warehouse.drugpurchase.service.DrugpurchaseService;
import com.hospital.warehouse.exception.WarehouseException;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Mr. YR
 * @since 2020-04-21
 */
@Service
public class DrugpurchaseServiceImpl extends ServiceImpl<DrugpurchaseDao, Drugpurchase> implements DrugpurchaseService {

	@Autowired
	DrugpurchaseDao drugpurchaseDao;

	@Override
	public DTO paging(Drugpurchase dp, Integer page, Integer limit) {
//		System.out.println(BigDecimal.ZERO.compareTo(dp.getDp_price_min()));
//		System.out.println(BigDecimal.ZERO.compareTo(dp.getDp_price_max()));

		LambdaQueryWrapper<Drugpurchase> wrapper = Wrappers.lambdaQuery();
//		wrapper.eq("dp_state", "等待确认");
//		wrapper.ge(BigDecimal.ZERO.compareTo(dp.getDp_price_min())==-1, Drugpurchase::getDp_price_min, dp.getDp_price_min())
//		.le(BigDecimal.ZERO.compareTo(dp.getDp_price_max())==-1, Drugpurchase::getDp_price_max, dp.getDp_price_max());

		Page<Drugpurchase> pageinfo = new Page<>(page, limit);

		IPage<Drugpurchase> iPage = drugpurchaseDao.paging(dp, pageinfo, wrapper);
		List<Drugpurchase> list = iPage.getRecords();

		DTO dto = new DTO();
		dto.setCount(iPage.getTotal());
		dto.setData(list);

		return dto;
	}

	@Override
	public Drugpurchase queryById(Integer dp_id) {
		Drugpurchase dp = drugpurchaseDao.queryById(dp_id);
		return dp;
	}

	@Override
	public List<Drugpurchase> queryByState(String state1,String state2) {
		List<Drugpurchase> list = drugpurchaseDao.queryByState(state1,state2);
		return list;
	}

	@Override
	public void saveOne(Drugpurchase dp, HttpSession session) {
//		Sys_user user = (Sys_user) session.getAttribute("user");
//		user.getUser_id();

		dp.setDp_applicantid(new Random().nextInt(2) + 1);
		dp.setDp_createtime(new Date());
		dp.setDp_state("等待确认");

		// 继承ServiceImpl后，相对于insert方法
		this.save(dp);
	}

	@Override
	public void updateOne(Drugpurchase dp) {
		if (dp == null || dp.getDp_id() == null) {
			throw new WarehouseException("不能为空");
		}
		this.lambdaUpdate().eq(Drugpurchase::getDp_state, "等待确认").eq(Drugpurchase::getDp_id, dp.getDp_id())
				.set(Drugpurchase::getDp_drugid, dp.getDp_drugid()).set(Drugpurchase::getDp_num, dp.getDp_num())
				.set(Drugpurchase::getDp_price, dp.getDp_price()).update();

//		Drugpurchase newDp = new Drugpurchase();
//		newDp.setDp_id(dp.getDp_id());
//		newDp.setDp_drugid(dp.getDp_drugid());
//		newDp.setDp_num(dp.getDp_num());
//		newDp.setDp_price(dp.getDp_price());
//		drugpurchaseDao.updateById(newDp);
	}

	@Override
	public void updateState(Integer id, String state, HttpSession session) {
		Drugpurchase dp = new Drugpurchase();
		if (JMyUtil.isEmpty(id)) {
			throw new WarehouseException("请选择采购单");
		}

//		Sys_user user = (Sys_user) session.getAttribute("user");
//		user.getUser_id();

		if ("confirm".equals(state)) {
			dp.setDp_state("正在采购");
		} else if ("cancel".equals(state)) {
			dp.setDp_state("已取消");
		} else {
			throw new WarehouseException("请选择状态");
		}
		dp.setDp_id(id);
		dp.setDp_confirmid(new Random().nextInt(2) + 1);
		dp.setDp_endtime(new Date());

		drugpurchaseDao.updateById(dp);
	}
}
